<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="名称">
        <el-input v-model="dict.name" />
      </el-form-item>
      <el-form-item label="编码">
        <el-input v-model="dict.dictCode" />
      </el-form-item>
      <el-form-item label="值">
        <el-input v-model="dict.value" />
      </el-form-item>
      <el-form-item v-if="$route.params.id != null">
        <el-button type="primary" @click="updateById">更新字典设置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>


<script>
import dict from "@/api/dict";
export default {
  data() {
    return {
      dict: {},
    };
  },
  created() {
    // 首先获取路由中是否存在参数 如果存在参数则执行 否则弹窗报错即可
    if (this.$route.params && this.$route.params.id) {
      const id = this.$route.params.id;
      this.selectById(id);
    }
  },
  methods: {
    selectById(id) {
      dict.selectById(id).then((response) => {
        if (response.code != 200) {
          this.$message({
            type: "error",
            message: `${response.message}！`,
          });
        } else {
          this.dict = response.data;
        }
      });
    },
    updateById() {
      dict.updateById(this.dict).then((response) => {
        if (response.code != 200) {
          this.$message({
            type: "error",
            message: `${response.message}！`,
          });
        } else {
          this.$message({
            type: "success",
            message: "更新字典成功!",
          });
          this.$router.push({ path: "/cmn/list" });
        }
      });
    },
  },
};
</script>